package edu.zac.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @Title
 * @Author Peihan.Zhang
 * @Email zhangpeihan@sjgtw.com
 * @Date 2021/10/22 10:15
 * @Versions 1.0
 */
public class ProxyFactory {

    public static Object getProxy( final  Object target){


        Proxy  proxy = (Proxy) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
            Object object=null;
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                long time= System.currentTimeMillis();
                System.out.println("事务开始");
                Object object=method.invoke(target, args);
                Thread.sleep(5000);
                long endtime= System.currentTimeMillis();

                System.out.println("事务结束");
                System.out.println(endtime-time);
                return object;
            }
        });
                return  proxy;
    }
}

